package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.javassist.tools.reflect.CannotCreateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     */
    @Override
    @Transactional
    public void setmealWithDish(SetmealDto setmealDto) {
        //操作setmeal表
        this.save(setmealDto);

        //设置setmeal_dish的setmealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        //批量添加
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     *分页查询
     */
    @Override
    public Page<SetmealDto> pageWithDto(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .eq(StringUtils.isNotBlank(name), Setmeal::getName, name);

        this.page(pageInfo1,queryWrapper);

        Page<SetmealDto> pageInfo2=new Page<>();
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        List<Setmeal> setmeals = pageInfo1.getRecords();

        List<SetmealDto> setmealDtos = setmeals.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        pageInfo2.setRecords(setmealDtos);

        return pageInfo2;
    }

    /**
     * 删除套餐
     */
    @Override
    @Transactional
    public void deleteWithDish(Long[] ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomerException("该套餐有正在售卖的,不能删除");
        }

        this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper);


    }

    /**
     * 回显数据
     */
    @Override
    public SetmealDto showSetmealDto(Long id) {

        Setmeal meal = this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(meal, setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId,meal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 修改
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改setmeal表
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);


        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //批量添加
        setmealDishService.saveBatch(setmealDishes);

    }


}
